//
//  UIColor+Extension.swift
//  AoNongOA
//
//  Created by Qiuxk
// on 16/7/14.
//  Copyright © 2016年 scale. All rights reserved.
//
//

import UIKit

extension UIColor {
    class func colorWithRGBHex(_ valueRGB: UInt) -> UIColor {
          let r = CGFloat((valueRGB & 0xFF0000) >> 16)
          let g = CGFloat((valueRGB & 0x00FF00) >> 8)
          let b = CGFloat(valueRGB & 0x0000FF)
          return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1.0)
      }
    
   static func turquoiseColor() -> CAGradientLayer {

        let leftColor = UIColor.colorWithRGBHex(0x79c2f5)
        let rightColor = UIColor.white
        // 颜色数组
        let gradientColors: Array < Any> = [leftColor.cgColor, rightColor.cgColor]
        // 分割点
        let gradientLocations: Array <NSNumber> = [0.1, 1.0]

        let gradientLayer: CAGradientLayer = CAGradientLayer()
        // 开始结束
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)

        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations

        return gradientLayer
    }
    static func blueWhiteColor() -> CAGradientLayer {

         let leftColor = UIColor.init(r: 80, g: 132, b: 247)
         let rightColor = UIColor.white
         // 颜色数组
         let gradientColors: Array < Any> = [leftColor.cgColor, rightColor.cgColor]
         // 分割点
         let gradientLocations: Array <NSNumber> = [0.0, 1.0]

         let gradientLayer: CAGradientLayer = CAGradientLayer()
         // 开始结束
         gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
         gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)

         gradientLayer.colors = gradientColors
         gradientLayer.locations = gradientLocations

         return gradientLayer
     }
    
    static func turquoiseColor_yellow() -> CAGradientLayer {

         let leftColor = UIColor.colorWithRGBHex(0xf4511e)
         let rightColor = UIColor.white
         // 颜色数组
         let gradientColors: Array < Any> = [leftColor.cgColor, rightColor.cgColor]
         // 分割点
         let gradientLocations: Array <NSNumber> = [0.1, 1.0]

         let gradientLayer: CAGradientLayer = CAGradientLayer()
         // 开始结束
         gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
         gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)

         gradientLayer.colors = gradientColors
         gradientLayer.locations = gradientLocations

         return gradientLayer
     }

    class func randomColor() -> UIColor {
        let r = CGFloat(arc4random_uniform(256))
        let g = CGFloat(arc4random_uniform(256))
        let b = CGFloat(arc4random_uniform(256))
        return UIColor(r:r, g: g, b: b)
    }
    class func lightRandomColor() -> UIColor { /// 控制颜色区间，较为浅色系
        let r = CGFloat(arc4random_uniform(128) + 90)
        let g = CGFloat(arc4random_uniform(128) + 90)
        let b = CGFloat(arc4random_uniform(128) + 90)
        return UIColor(r:r, g: g, b: b)
    }
    class func darkRandomColor() -> UIColor { /// 控制颜色区间，较为深色系
        let r = CGFloat(arc4random_uniform(100) + 20)
        let g = CGFloat(arc4random_uniform(100) + 20)
        let b = CGFloat(arc4random_uniform(100) + 20)
        return UIColor(r:r, g: g, b: b)
    }
    //给市场调研地图用 的
    class func dyRandomColor() -> UIColor { /// 控制颜色区间，较为深色系
        let r = CGFloat(arc4random_uniform(200) + 20)
        let g = CGFloat(arc4random_uniform(200) + 20)
        let b = CGFloat(arc4random_uniform(200) + 20)
        return UIColor(r:r, g: g, b: b)
    }
 
    convenience init(kshex: String) {
        var cString: String = kshex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
        if cString.hasPrefix("#") {
            cString = String(cString[cString.index(cString.startIndex, offsetBy: 1)...])
        }

        if cString.count != 6 {
            cString = "000000"
        }
            var rgbValue: UInt32 = 0
            Scanner(string: cString).scanHexInt32(&rgbValue)

            self.init(
                red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
                green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
                blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
                alpha: CGFloat(1.0)
            )
       
    }

    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
        self.init(red: CGFloat(r / 255.0), green: CGFloat(g / 255.0), blue: CGFloat(b / 255.0), alpha: CGFloat(1))
    }

}
